<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>角色管理</title>
    <link href="../../css/element.css" rel="stylesheet">
    <link href="../../css/base.css" rel="stylesheet">
</head>
<body>
<div id="wrapper" v-cloak>
    <el-container>
        <el-aside :width="isCollapse ? '64px' : '200px'" style="background-color: black; transition:width .5s">
            <div style="background-color: black; height: 60px; line-height: 60px; text-align: center;
                color: white; font-size: 20px;">
                <transition name="el-fade-in-linear">
                    <span name="fade" v-show="!isCollapse"><a href="/page/end/index.html">X-Admin</a></span>
                </transition>
            </div>
            <el-menu :collapse="isCollapse" :default-openeds="['1']" active-text-color="#ffd04b" background-color="black"
                     class="el-menu-vertical-demo"
                     default-active="role"
                     style="border: none;" text-color="#fff">
                <a href="/page/end/index.html">
                    <el-menu-item index="home">
                        <i class="el-icon-data-line"></i>
                        <span slot="title">首页</span>
                    </el-menu-item>
                </a>
                <el-submenu index="1" v-if="user.permission && user.permission.length > 0">
                    <template slot="title">
                        <i class="el-icon-menu"></i>
                        <span slot="title">信息管理</span>
                    </template>
                    <a :href="item.path" :key="item.id" v-for="item in user.permission">
                        <el-menu-item :index="item.flag">
                            <i class="el-icon-s-data"></i>
                            <span slot="title">{{item.name}}</span>
                        </el-menu-item>
                    </a>
                </el-submenu>
            </el-menu>
        </el-aside>
        <el-container>
            <el-header style="background-color: black; line-height: 60px; color: white;">
                <el-row>
                    <el-col :span="1">
                        <i :class="[isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold']"
                           @click="handleCollapse" style="font-size: 22px; cursor: pointer"></i>
                    </el-col>
                    <el-col :offset="21" :span="2" style="text-align: right">
                        <span style="color: white; margin-right: 10px">{{user.username}}</span>
                        <el-dropdown>
                            <img src="/files/avatar"
                                 style="width: 40px; height: 40px; margin-right: 10px; border-radius: 50%">
                            <el-dropdown-menu slot="dropdown">
                                <a href="/page/end/person.html"
                                   style="display:inline-block; padding: 5px 0; width: 100px; text-align: center; color: black">个人信息</a>
                                <a @click="logout" href="#"
                                   style="display:block; width: 100px;  text-align: center; color: black">退出</a>
                            </el-dropdown-menu>
                        </el-dropdown>
                    </el-col>
                </el-row>
            </el-header>
            <!-- 主体区域 -->
            <el-main>
                <div style="color: black; font-size: 20px; margin: 10px 0">角色管理</div>
                <div style="height: 1px; background-color: white"></div>
                <el-input @keyup.enter.native="loadTable" placeholder="请输入名称按回车搜索" style="width: 20%;"
                          suffix-icon="el-icon-search"
                          v-model="search"></el-input>
                <el-button @click="add" size="mini" style="margin: 10px 0" type="primary">新增</el-button>
                <!-- <el-button @click="exp" type="primary" size="mini" style="margin: 10px 0">导出</el-button> -->
                <el-table
                        :data="tableData"
                        border
                        style="width: 100%">
                    <el-table-column
                            :label="item.label"
                            :prop="item.prop"
                            v-for="item in props"
                    >
                    </el-table-column>
                    <el-table-column
                            label="权限">
                        <template slot-scope="scope">
                            <el-select @change="changePer(scope.row)" multiple placeholder="请选择" v-model="scope.row.permission"
                                       value-key="id">
                                <el-option
                                        :key="item.id"
                                        :label="item.name"
                                        :value="item"
                                        v-for="item in options">
                                </el-option>
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column
                            fixed="right"
                            label="操作"
                            width="100">
                        <template slot-scope="scope">
                            <el-button @click="edit(scope.row)" type="text">编辑</el-button>
                            <el-popconfirm
                                    @onConfirm="del(scope.row.id)"
                                    title="确定删除？"
                            >
                                <el-button slot="reference" type="text">删除</el-button>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>
                <!-- 分页 -->
                <div style="background-color: white">
                    <el-pagination
                            :current-page="pageNum"
                            :page-size="pageSize"
                            :page-sizes="[5, 10, 20, 40]"
                            :total="total"
                            @current-change="handleCurrentChange"
                            @size-change="handleSizeChange"
                            layout="total, sizes, prev, pager, next, jumper">
                    </el-pagination>
                </div>

                <el-dialog :visible.sync="dialogFormVisible" close-on-click-modal="false" close-on-press-escape="false"
                           show-close="false" title="角色信息" width="30%">
                    <el-form :model="entity">
                        <el-form-item :label="item.label" label-width="100px" v-for="item in props"
                                      v-if="item.prop !== 'id'">
                            <el-input autocomplete="off" style="width: 80%" v-model="entity[item.prop]"></el-input>
                        </el-form-item>
                    </el-form>
                    <div class="dialog-footer" slot="footer">
                        <el-button @click="dialogFormVisible = false">取 消</el-button>
                        <el-button @click="save" type="primary">确 定</el-button>
                    </div>
                </el-dialog>

            </el-main>
        </el-container>
    </el-container>
</div>

<script src="../../js/jquery.min.js"></script>
<script src="../../js/vue.min.js"></script>
<script src="../../js/element.js"></script>
<script src="../../js/tinymce/tinymce.min.js"></script>
<script src="../../js/base.js"></script>

<script>

    let urlBase = "/role/";
    new Vue({
        el: "#wrapper",
        data: {
            user: {},
            tableData: [],
            pageNum: 1,
            pageSize: 10,
            total: 0,
            dialogFormVisible: false,
            entity: {},
            isCollapse: false,
            search: '',
            props: [{"label": "ID", "prop": "id"}, {"label": "名称", "prop": "name"}, {
                "label": "描述",
                "prop": "description"
            }],
            options: []
        },
        created() {
            this.user = sessionStorage.getItem("user") ? JSON.parse(sessionStorage.getItem("user")) : {};
            this.loadTable();


        },
        methods: {
            handleCollapse() {
                this.isCollapse = !this.isCollapse;
            },
            logout() {
                $.get("/user/logout");
                sessionStorage.removeItem("user");
                location.href = "/page/end/login.html";
            },
            loadTable() {
                $.get(urlBase + "/page?pageNum=" + this.pageNum + "&pageSize=" + this.pageSize + "&name=" + this.search).then(res => {
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                });

                $.get("/permission").then(res => {
                    this.options = res.data;
                })
            },
            exp() {
                window.open(urlBase + "/export");
            },
            handleSizeChange(pageSize) {
                this.pageSize = pageSize;
                this.loadTable();
            },
            handleCurrentChange(pageNum) {
                this.pageNum = pageNum;
                this.loadTable();
            },
            add() {
                this.entity = {};
                this.dialogFormVisible = true;
            },
            save() {
                let type = this.entity.id ? "PUT" : "POST";
                $.ajax({
                    url: urlBase,
                    type: type,
                    contentType: "application/json",
                    data: JSON.stringify(this.entity)
                }).then(res => {
                    if (res.code === '0') {
                        this.$message({
                            message: "保存成功",
                            type: "success"
                        });
                        this.loadTable();
                    } else {
                        this.$message({
                            message: res.msg,
                            type: "error"
                        })
                    }
                    this.dialogFormVisible = false;

                    // 重新请求用户基础数据
                    $.get("/user/detail/" + this.user.username).then(res => {
                        this.user = res.data;
                        sessionStorage.setItem("user", JSON.stringify(this.user));
                    })
                })
            },
            edit(obj) {
                this.entity = obj;
                this.dialogFormVisible = true;
            },
            del(id) {
                $.ajax({
                    url: urlBase + id,
                    type: "delete"
                }).then(res => {
                    if (res.code === '0') {
                        this.$message({
                            message: "删除成功",
                            type: "success"
                        })
                        this.loadTable();
                    } else {
                        this.$message({
                            message: res.msg,
                            type: "error"
                        })
                    }
                })
            },
            changePer(obj) {
                this.entity = obj;
                this.save();
            }
        }
    })
</script>
</body>
</html>
